# Locate PCRE library
# This module exports the following targets
#
# PCRE::PCRE
#
# This module defines
#  PCRE_FOUND, if false, do not try to link to PCRE
#  PCRE_LIBRARIES
#  PCRE_INCLUDE_DIR, where to find pcre.h

FIND_PACKAGE(PkgConfig)

PKG_SEARCH_MODULE(PC_PCRE pcre libpcre)

FIND_PATH(PCRE_INCLUDE_DIR pcre.h
  HINTS
    ${PCRE_DIR}
    $ENV{PCRE_DIR}
    ${PC_PCRE_INCLUDE_DIRS}
  PATH_SUFFIXES
    include/pcre
    include
  PATHS
    ~/Library/Frameworks
    /Library/Frameworks
    /usr/local
    /usr
    /sw # Fink
    /opt/local # DarwinPorts
    /opt/csw # Blastwave
    /opt
)

FIND_LIBRARY(PCRE_LIBRARY_RELEASE
  NAMES
    pcre
  HINTS
    ${PCRE_DIR}
    $ENV{PCRE_DIR}
    ${PC_PCRE_LIBRARY_DIRS}
    ${PC_PCRE_LIBRARY_DIR}
  PATH_SUFFIXES
    lib64
    lib
  PATHS
    ~/Library/Frameworks
    /Library/Frameworks
    /usr/local
    /usr
    /sw
    /opt/local
    /opt/csw
    /opt
)

FIND_LIBRARY(PCRE_LIBRARY_DEBUG
  NAMES
    pcred
  HINTS
    ${PCRE_DIR}
    $ENV{PCRE_DIR}
    ${PC_PCRE_LIBRARY_DIRS}
    ${PC_PCRE_LIBRARY_DIR}
  PATH_SUFFIXES
    lib64
    lib
  PATHS
    ~/Library/Frameworks
    /Library/Frameworks
    /usr/local
    /usr
    /sw
    /opt/local
    /opt/csw
    /opt
)

IF(PCRE_LIBRARY_DEBUG AND PCRE_LIBRARY_RELEASE)
  SET(PCRE_LIBRARY optimized ${PCRE_LIBRARY_RELEASE} debug ${PCRE_LIBRARY_DEBUG} )
ELSEIF(PCRE_LIBRARY_RELEASE)
  SET(PCRE_LIBRARY ${PCRE_LIBRARY_RELEASE} )
ELSEIF(PCRE_LIBRARY_DEBUG)
  SET(PCRE_LIBRARY ${PCRE_LIBRARY_DEBUG} )
ENDIF()

IF(PC_PCRE_pcre_FOUND)
  SET(PCRE_VERSION ${PC_PCRE_pcre_VERSION})
ELSEIF(PC_PCRE_libpcre_FOUND)
  SET(PCRE_VERSION ${PC_PCRE_libpcre_VERSION})
ELSE()
  SET(PCRE_VERSION ${PC_PCRE_VERSION})
ENDIF()

INCLUDE(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set PCRE_FOUND to TRUE if
# all listed variables are TRUE
FIND_PACKAGE_HANDLE_STANDARD_ARGS(PCRE
  REQUIRED_VARS
    PCRE_LIBRARY
    PCRE_INCLUDE_DIR
  VERSION_VAR
    PCRE_VERSION
)

MARK_AS_ADVANCED(PCRE_INCLUDE_DIR PCRE_LIBRARY PCRE_LIBRARY_RELEASE PCRE_LIBRARY_DEBUG)

GET_FILENAME_COMPONENT(PCRE_FILENAME ${PCRE_LIBRARY} NAME_WE)
STRING(FIND ${PCRE_FILENAME} .a PCRE_STATIC)

IF(PCRE_FOUND AND NOT TARGET PCRE::PCRE)
  if(PCRE_STATIC EQUAL -1)
    ADD_LIBRARY(PCRE::PCRE SHARED IMPORTED)
    SET_TARGET_PROPERTIES(PCRE::PCRE PROPERTIES
      IMPORTED_LOCATION
        "${PCRE_LIBRARY}"
      INTERFACE_INCLUDE_DIRECTORIES
        "${PCRE_INCLUDE_DIR}"
    )
  ELSE()
    ADD_LIBRARY(PCRE::PCRE STATIC IMPORTED)
    SET_TARGET_PROPERTIES(PCRE::PCRE PROPERTIES
      INTERFACE_COMPILE_DEFINITIONS
        PCRE_STATIC
      IMPORTED_LOCATION
        "${PCRE_LIBRARY}"
      INTERFACE_INCLUDE_DIRECTORIES
        "${PCRE_INCLUDE_DIR}"
    )
  ENDIF()
ENDIF()